#import <Foundation/Foundation.h>
#import "TTTableNeedItem.h"
#import "TTTableNeedItemCell.h"
#import "TTTableHelpItem.h"
#import "TTTableHelpItemCell.h"
#import "INDefaultStylesheet.h"
#import "NeedsViewDataSource.h"
#import "NeedsModel.h"
#import "Need.h"


#define POST_ROW_HEIGHT         120
#define kSectionHeaderHight     25.0

@implementation NeedsViewDataSource

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (void)tableViewDidLoadModel:(UITableView *)tableView
{
	
    [super tableViewDidLoadModel:tableView];
	[tableView setEditing:NO animated:YES];
	
    [self.items removeAllObjects];
	
    // Construct an object that is suitable for the table view system
    // from each SearchResult domain object that we retrieve from the TTModel.
    for (Need *result in [(id<NeedsModel>)self.model results]) {
		
		NSString *needUrl = [NSString stringWithFormat:@"in://needView/?need=%@", result._id];
		
        [self.items addObject:[TTTableNeedItem itemWithText:result.text 
												displayname:result.user.displayname 
													subject:result.subject 
												  timestamp:[NSDate dateWithTimeIntervalSinceNow:[result.timediff doubleValue]]
												 pictureurl:result.user.pictureurl
													  city:result.city
													   helps:result.helps
														URL:needUrl]];
		
//        [self.items addObject:[TTTableHelpItem itemWithText:[TTStyledText textFromXHTML:result.text] 
//												displayname:result.user.displayname 
//													subject:result.subject 
//												  timestamp:[NSDate dateWithTimeIntervalSinceNow:[result.timediff doubleValue]]
//												 pictureurl:result.user.pictureurl
//													   city:result.city
//														URL:needUrl]];
//		TTLOG(@"Added %@ need ", result.helps);
	
	}
	
//    NSLog(@"Added %lu search result objects", (unsigned long)[self.items count]);
}

///////////////////////////////////////////////////////////////////////////////////////////////////

- (void)dealloc {
	[super dealloc];
}

- (Class)tableView:(UITableView*)tableView cellClassForObject:(id) object { 
	
	if ([object isKindOfClass:[TTTableNeedItem class]]) { 
		return [TTTableNeedItemCell class]; 		
	} else { 
		return [super tableView:tableView cellClassForObject:object]; 
	}
//	
//	if ([object isKindOfClass:[TTTableHelpItem class]]) { 
//		return [TTTableHelpItemCell class]; 		
//	} else { 
//		return [super tableView:tableView cellClassForObject:object]; 
//	}
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
}


- (void)tableView:(UITableView*)tableView prepareCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//	cell.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);

//	cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//	cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

}

@end
